package com.wetao.module_homework.adapter;


import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wetao.lib_common.bean.vo.QuestionNumShow;
import com.wetao.lib_common.greendao.model.AssistantQuestion;
import com.wetao.lib_common.utils.MultiClickUtil;
import com.wetao.module_homework.R;
import com.wetao.module_homework.R2;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class HomeworkNumAdapter extends RecyclerView.Adapter{
    private List<QuestionNumShow> mData= new ArrayList<>();
    private int mCurPosition = -1;
    private int mQuestionId;
    public HomeworkNumAdapter() {
    }

    public void updateData(List<QuestionNumShow> data) {
        mCurPosition = -1; //重置课时时，需要重置
        mData = data;
        notifyDataSetChanged();
    }

    private void setmCurPosition(int position) {
        if(mCurPosition != position) {
            mCurPosition = position;
            notifyDataSetChanged();
            if(listener != null) {
                listener.onPositionChange(mCurPosition);
                if(mCurPosition == mData.size() - 1) {
                    listener.onIsLastQuestion(true);
                } else {
                    listener.onIsLastQuestion(false);
                }
            }
        }
    }

    public void setmQuestionId(int questionId) {
        mQuestionId = questionId;
        for(int i = 0; i < mData.size(); i++) {
            for(int j = 0; j < mData.get(i).getQuestionList().size(); j++) {
                if(mQuestionId == mData.get(i).getQuestionList().get(j).getQuestionId().intValue()) {
                    setmCurPosition(i);
                    return;
                }
            }
        }
    }

    public List<AssistantQuestion> getCurPostionData() {
        return mData.get(mCurPosition).getQuestionList();
    }

    public AssistantQuestion getQuestion(int position) {
        //不能使用mCurPosition, 这个是上一次显示的
        return mData.get(position).getQuestionList().get(0);
    }

    public AssistantQuestion getNextQuestion() {
        //不能使用mCurPosition, 这个是上一次显示的
        return mData.get(mCurPosition + 1).getQuestionList().get(0);
    }

    public int getNextPosition() {
        return mCurPosition + 1;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == 1) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.homework_num_item, parent, false);
            return new NumHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof NumHolder) {
            ((NumHolder)holder).bindData(position);
        }
    }

    @Override
    public int getItemCount() {
        if(mData == null) {
            return 0;
        }
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }


    public class NumHolder extends RecyclerView.ViewHolder{

        @BindView(R2.id.tv_num)
        TextView tv_num;

        public NumHolder( View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void bindData(int position) {
            clickListener(position);
            tv_num.setText(mData.get(position).getStrNum());
            if(position == mCurPosition) {
                tv_num.setTextColor(Color.parseColor("#FFFFFF"));
                tv_num.setBackgroundResource(R.drawable.common_question_num_3);
            } else {
                tv_num.setTextColor(Color.parseColor("#333333"));
                tv_num.setBackground(null);
            }
        }
        private void clickListener(int position) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null && mCurPosition != position) {
                        //mCurPosition = position;
                        //notifyDataSetChanged();
                        listener.onItemClick(position);
                    }
                }
            });
        }
    }

    //第一步 定义接口
    public interface OnItemClickListener {
        void onItemClick(int position);
        void onPositionChange(int position);
        void onIsLastQuestion(boolean isLastQuestion);
    }
    private OnItemClickListener listener;
    //第二步， 写一个公共的方法
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

}
